<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<META name="generator" content="HTML Tidy for Linux/x86 (vers 12 April 2005), see www.w3.org">
<TITLE>Scripts</TITLE>
<META name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.79">
<LINK rel="HOME" title="Abyss Web Server For Windows User's Guide" href="index.html">
<LINK rel="UP" title="Custom Directory Listings" href="dirlist.html">
<LINK rel="PREVIOUS" title="Custom Directory Listings" href="dirlist.html">
<LINK rel="NEXT" title="Glossary" href="glossary.html">
<LINK rel="STYLESHEET" type="text/css" href="stylesheet.css">
<META name="AppleIcon" content="icon.png">
<META name="AppleTitle" content="Scripts">
<META name="Description" content="Scripts">
<META name="AppleOrder" content="">
</HEAD>
<BODY class="SECT1" bgcolor="#FFFFFF" text="#000000">
<DIV class="NAVHEADER">
<TABLE summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<TR>
<TH colspan="3" align="center">Abyss Web Server For Windows User's Guide</TH>
</TR>
<TR>
<TD width="10%" align="left" valign="bottom"><A href="dirlist.html" accesskey="P">Prev</A></TD>
<TD width="80%" align="center" valign="bottom">Chapter 8. Custom Directory Listings</TD>
<TD width="10%" align="right" valign="bottom"><A href="glossary.html" accesskey="N">Next</A></TD>
</TR>
</TABLE>
<HR align="left" width="100%"></DIV>
<DIV class="SECT1">
<H1 class="SECT1"><A name="DIRSCRIPT" id="DIRSCRIPT">Scripts</A></H1>
<P>When a directory listing is generated through a script, the server gathers the information related to the files inside the listed directory and packs it in a special string. Then the script is executed and the special string is <SPAN class="emphasis"><I class="EMPHASIS">POST</I></SPAN>ed to it inside a variable called <KBD class="USERINPUT">files</KBD>. The server also <SPAN class="emphasis"><I class="EMPHASIS">POST</I></SPAN>s the directory virtual path in a variable which name is <KBD class="USERINPUT">path</KBD> as well as an URL encoded (escaped) version of the virtual path in a variable named <KBD class="USERINPUT">encoded_path</KBD>.</P>
<P>This special string passed in the <KBD class="USERINPUT">files</KBD> variable is of the form:</P>
<DIV class="INFORMALTABLE"><A name="AEN2670" id="AEN2670"></A>
<TABLE border="0" frame="void" class="CALSTABLE">
<COL width="96">
<COL width="96">
<COL width="96">
<COL width="96">
<COL width="96">
<TBODY>
<TR>
<TD>name<SUB>1</SUB>&rarr;</TD>
<TD>url<SUB>1</SUB>&rarr;</TD>
<TD>size<SUB>1</SUB>&rarr;</TD>
<TD>date<SUB>1</SUB>&rarr;</TD>
<TD>MIME-type<SUB>1</SUB>[CR]</TD>
</TR>
<TR>
<TD>name<SUB>2</SUB>&rarr;</TD>
<TD>url<SUB>2</SUB>&rarr;</TD>
<TD>size<SUB>2</SUB>&rarr;</TD>
<TD>date<SUB>2</SUB>&rarr;</TD>
<TD>MIME-type<SUB>2</SUB>[CR]</TD>
</TR>
<TR>
<TD>.</TD>
<TD>.</TD>
<TD>.</TD>
<TD>.</TD>
<TD>&nbsp;</TD>
</TR>
<TR>
<TD>.</TD>
<TD>.</TD>
<TD>.</TD>
<TD>.</TD>
<TD>&nbsp;</TD>
</TR>
<TR>
<TD>name<SUB>n-1</SUB>&rarr;</TD>
<TD>url<SUB>n-1</SUB>&rarr;</TD>
<TD>size<SUB>n-1</SUB>&rarr;</TD>
<TD>date<SUB>n-1</SUB>&rarr;</TD>
<TD>MIME-type<SUB>n-1</SUB>[CR]</TD>
</TR>
<TR>
<TD>name<SUB>n</SUB>&rarr;</TD>
<TD>url<SUB>n</SUB>&rarr;</TD>
<TD>size<SUB>n</SUB>&rarr;</TD>
<TD>date<SUB>n</SUB>&rarr;</TD>
<TD>MIME-type<SUB>n</SUB></TD>
</TR>
</TBODY>
</TABLE>
</DIV>
<P>&rarr; represents the tabulation character (ASCII code 9 - <KBD class="USERINPUT">\t</KBD>) and [CR] represents the carriage return character (ASCII code 13 - <KBD class="USERINPUT">\n</KBD>). The tuple <KBD class="USERINPUT">(name<SUB>i</SUB>, url<SUB>i</SUB>, size<SUB>i</SUB>, date<SUB>i</SUB>, MIME-type<SUB>i</SUB>)</KBD> is the information related to the i<SUP>th</SUP> file in the listing.</P>
<P>The name and the URL of every file are UTF-8 encoded. The size is in bytes. The date is conforming to the format <KBD class="USERINPUT">YYYY-MM-DD hh:mm:ss</KBD> which is equivalent in <KBD class="USERINPUT">strftime</KBD> semantics to <KBD class="USERINPUT">%Y-%m-%d %H:%M:%S</KBD>.</P>
<P>The MIME type of a directory is empty, i.e. it is equal to the empty string. The name of a directory contains always a trailing slash.</P>
<P>The script should split the <KBD class="USERINPUT">files</KBD> variable value on the carriage return characters then split each line on the tabulation character to have the information related to each file.</P>
<DIV class="EXAMPLE"><A name="AEN2749" id="AEN2749"></A>
<P><B>Example 8-2. A very simple directory listing script (PHP version)</B></P>
<PRE class="PROGRAMLISTING">
   &lt;HTML&gt;
     &lt;HEAD&gt;
       &lt;META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"&gt;
       &lt;TITLE&gt;
           Index of &lt;?php echo $_POST['path']; ?&gt;
       &lt;/TITLE&gt;
     &lt;/HEAD&gt;
   
     &lt;BODY&gt;
   
       &lt;TABLE BORDER="0"&gt;
   
         &lt;TR&gt;
           &lt;TD&gt;Name&lt;/TD&gt;
           &lt;TD&gt;Size&lt;/TD&gt;
           &lt;TD&gt;Date&lt;/TD&gt;
           &lt;TD&gt;MIME Type&lt;/TD&gt;
         &lt;/TR&gt;
   
         &lt;?php
   
           /* Split and get the lines */
           $lines = explode("\n", $_POST['files']);
   
           /* For each line do... */
           foreach ($lines as $line)
           {
             /* Split the line and get the file information */
             list($name, $url, $size, $date, $mimetype) = explode("\t", $line);
          
             if ($mimetype == "")
               $mimetype = "Directory";
   
             echo "&lt;TR&gt;&lt;TD&gt;&lt;A HREF=\"$url\"&gt;" . htmlentities($name) .
                       "&lt;/A&gt;&lt;/TD&gt;&lt;TD&gt;$size&lt;/TD&gt;&lt;TD&gt;$date&lt;/TD&gt;&lt;TD&gt;$mimetype&lt;/TD&gt;&lt;/TR&gt;";
           }
        ?&gt;
   
        &lt;/TABLE&gt;
      &lt;/BODY&gt;
   &lt;/HTML&gt;
</PRE></DIV>
<DIV class="EXAMPLE"><A name="AEN2752" id="AEN2752"></A>
<P><B>Example 8-3. A very simple directory listing script (Python version)</B></P>
<PRE class="PROGRAMLISTING">
   import cgi, string, os
   
   posted_data = cgi.FieldStorage()
   
   # Write the CGI header
   print "Content-Type: text/html; charset=utf-8"
   print
   
   print "&lt;HTML&gt;&lt;HEAD&gt;"
   print "&lt;META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\"&gt;"
   print "&lt;TITLE&gt;Index of %s&lt;/TITLE&gt;&lt;/HEAD&gt;" % posted_data["path"].value
   
   print "&lt;TABLE BORDER='0'&gt;&lt;TR&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Size&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;MIME Type&lt;/TD&gt;&lt;/TR&gt;"
   
   # Split and get the lines
   lines = (posted_data["files"].value).split("\n")
   
   # for each line do...
   for line in lines:
           # Split the line and get the file information
           (name, url, size, date, mimetype) = line.split("\t")
   
           if (mimetype == ""):
                   mimetype = "Directory"
   
           print "&lt;TR&gt;&lt;TD&gt;&lt;A HREF=\"%s\"&gt;%s&lt;/A&gt;&lt;/TD&gt;" % ( url, cgi.escape(name) )
           print "&lt;TD&gt;%d&lt;/TD&gt;&lt;TD&gt;%s&lt;/TD&gt;&lt;TD&gt;%s&lt;/TD&gt;&lt;/TR&gt;" % (int(size), date, mimetype)
   
   print "&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;"
</PRE></DIV>
</DIV>
<DIV class="NAVFOOTER">
<HR align="left" width="100%">
<TABLE summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<TR>
<TD width="33%" align="left" valign="top"><A href="dirlist.html" accesskey="P">Prev</A></TD>
<TD width="34%" align="center" valign="top"><A href="index.html" accesskey="H">Home</A></TD>
<TD width="33%" align="right" valign="top"><A href="glossary.html" accesskey="N">Next</A></TD>
</TR>
<TR>
<TD width="33%" align="left" valign="top">Custom Directory Listings</TD>
<TD width="34%" align="center" valign="top"><A href="dirlist.html" accesskey="U">Up</A></TD>
<TD width="33%" align="right" valign="top">Glossary</TD>
</TR>
</TABLE>
</DIV>
<DIV class="COPYRIGHT">Copyright &copy; 2001-2009 Aprelium</DIV>
</BODY>
</HTML>
